package com.tanhua.server.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.TestSoulApi;
import com.tanhua.dubbo.server.pojo.*;
import com.tanhua.server.pojo.User;
import com.tanhua.server.utils.UserThreadLocal;
import com.tanhua.server.vo.ReportVo;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class TestSoulService {

    @Reference
    private TestSoulApi testSoulApi;

    //问卷列表
    public List<Paper> findQuestionList() {
        //查询当前登录用户
        User user = UserThreadLocal.get();

        //获取问卷列表
        List<Paper> papers = testSoulApi.findPapers();
        //获取userId
        List<Report> reports = testSoulApi.findByUserId(user.getId());

        //如果该用户没有报告，则直接返回题目列表，初级默认解锁
        if (reports == null || reports.size() == 0){
            return papers;
        }

        List<Paper> list = new ArrayList<>();
        //如果有一个report，则解锁中级问卷
        if (reports.size() == 1){
            for (Paper paper : papers) {
                if ("初级".equals(paper.getLevel())){
                    paper.setReportId(reports.get(0).getId().toHexString());
                }
                if ("中级".equals(paper.getLevel())){
                    paper.setIsLock(0);
                }
                list.add(paper);
            }
            //有两个及以上解锁高级问卷
        }else if (reports.size() == 2){
            for (Paper paper : papers) {
                if ("初级".equals(paper.getLevel())){
                    paper.setReportId(reports.get(0).getId().toHexString());
                }
                if ("中级".equals(paper.getLevel())){
                    paper.setReportId(reports.get(0).getId().toHexString());
                    paper.setIsLock(0);
                }
                if ("高级".equals(paper.getLevel())){
                    paper.setIsLock(0);
                }
                list.add(paper);
            }
        }else if (reports.size() == 3){
            for (Paper paper : papers) {
                if ("初级".equals(paper.getLevel())){
                    paper.setReportId(reports.get(0).getId().toHexString());
                }
                if ("中级".equals(paper.getLevel())){
                    paper.setReportId(reports.get(0).getId().toHexString());
                    paper.setIsLock(0);
                }
                if ("高级".equals(paper.getLevel())){
                    paper.setReportId(reports.get(0).getId().toHexString());
                    paper.setIsLock(0);
                }
                list.add(paper);
            }
        }

        return list;
    }

    //提交问卷
    public ResponseEntity commitPaper(Map<String,List<AnswerList>> map) {
        //获取当前用户信息
        User user = UserThreadLocal.get();

        //创建map集合存questionId和optionId
        List<AnswerList> answers = map.get("answers");

        Integer grade = 0;

        String questionId = null;
        //遍历问题和选项，对分数进行累加
        for (AnswerList answer : answers) {
            questionId = answer.getQuestionId();
            String optionId = answer.getOptionId();

            grade += testSoulApi.computeScore(questionId,optionId);

        }
        //获取分数后存入report
        Report report = new Report();
        report.setUserId(user.getId());

        //判断究竟是初级 中级还是高级试题
        if (Integer.valueOf(questionId) <= 10) {
            report.setType(1);
        } else if (Integer.valueOf(questionId) <= 20) {
            report.setType(2);
        } else {
            report.setType(3);
        }

        //根据分数判断结果类型
        if (grade < 21) {
            report.setConclusion("猫头鹰：他们的共同特质为重计划、条理、细节精准。在行为上，表现出喜欢理性思考与分析、较重视制度、结构、规范。他们注重执行游戏规则、循规蹈矩、巨细靡遗、重视品质、敬业负责。");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/owl.png");
            List<Dimension> dimensions = new ArrayList<>();
            dimensions.add(new Dimension("外向", "80%"));
            dimensions.add(new Dimension("判断", "70%"));
            dimensions.add(new Dimension("抽象", "90%"));
            dimensions.add(new Dimension("理性", "60%"));
            report.setDimensions(dimensions);

            List<Similar> similars = new ArrayList<>();
            similars.add(new Similar(1, " https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_1.png"));
            report.setSimilarYou(similars);

        } else if (grade < 40) {
            report.setConclusion("白兔型：平易近人、敦厚可靠、避免冲突与不具批判性。在行为上，表现出不慌不忙、冷静自持的态度。他们注重稳定与中长程规划，现实生活中，常会反思自省并以和谐为中心，即使面对困境，亦能泰然自若，从容应付。");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/rabbit.png");
            List<Dimension> dimensions = new ArrayList<>();
            dimensions.add(new Dimension("外向", "70%"));
            dimensions.add(new Dimension("判断", "80%"));
            dimensions.add(new Dimension("抽象", "60%"));
            dimensions.add(new Dimension("理性", "90%"));
            report.setDimensions(dimensions);

            List<Similar> similars = new ArrayList<>();
            similars.add(new Similar(1, " https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_2.png"));
            report.setSimilarYou(similars);
        } else if (grade < 55) {
            report.setConclusion("狐狸型 ：人际关系能力极强，擅长以口语表达感受而引起共鸣，很会激励并带动气氛。他们喜欢跟别人互动，重视群体的归属感，基本上是比较「人际导向」。由于他们富同理心并乐于分享，具有很好的亲和力，在服务业、销售业、传播业及公共关系等领域中，狐狸型的领导者都有很杰出的表现。");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/fox.png");
            List<Dimension> dimensions = new ArrayList<>();
            dimensions.add(new Dimension("外向", "80%"));
            dimensions.add(new Dimension("判断", "70%"));
            dimensions.add(new Dimension("抽象", "90%"));
            dimensions.add(new Dimension("理性", "60%"));
            report.setDimensions(dimensions);

            List<Similar> similars = new ArrayList<>();
            similars.add(new Similar(1, " https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_3.png"));
            report.setSimilarYou(similars);
        } else {
            report.setConclusion("狮子型：性格为充满自信、竞争心强、主动且企图心强烈，是个有决断力的领导者。一般而言，狮子型的人胸怀大志，勇于冒险，看问题能够直指核心，并对目标全力以赴。他们在领导风格及决策上，强调权威与果断，擅长危机处理，此种性格最适合开创性与改革性的工作。");
            report.setCover("https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/test_soul/lion.png");
            List<Dimension> dimensions = new ArrayList<>();
            dimensions.add(new Dimension("外向", "80%"));
            dimensions.add(new Dimension("判断", "70%"));
            dimensions.add(new Dimension("抽象", "90%"));
            dimensions.add(new Dimension("理性", "60%"));
            report.setDimensions(dimensions);

            List<Similar> similars = new ArrayList<>();
            similars.add(new Similar(1, " https://tanhua-dev.oss-cn-zhangjiakou.aliyuncs.com/images/tanhua/avatar_4.png"));
            report.setSimilarYou(similars);
        }
        //返回reportId
        String reportId = testSoulApi.saveReport(report);

        return ResponseEntity.ok(reportId);
    }

    public ResponseEntity report(String reportId) {
        Report report = testSoulApi.findById(reportId);
        ReportVo vo = new ReportVo();
        BeanUtils.copyProperties(report, vo);
        return ResponseEntity.ok(vo);
    }
}
